{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import lasio\n",
    "import os\n",
    "\n",
    "las = lasio.read(os.path.join(\"..\", \"tests\", \"examples\", \"6038187_v1.2_short.las\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The information in a LAS file's header is all parsed and available through the sections of the header.\n",
    "\n",
    "All the sections are in the dictionary ``las.sections``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Version', <class 'lasio.las_items.SectionItems'>)\n",
      "('Well', <class 'lasio.las_items.SectionItems'>)\n",
      "('Curves', <class 'lasio.las_items.SectionItems'>)\n",
      "('Parameter', <class 'lasio.las_items.SectionItems'>)\n",
      "('Other', <class 'str'>)\n"
     ]
    }
   ],
   "source": [
    "for name, section in las.sections.items():\n",
    "    print((name, type(section)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These standard section names are hard-coded as attributes, but if your LAS file has non-standard ones they will be in here too.\n",
    "\n",
    "Only the standard sections are parsed in the LAS format, as a ``SectionItems`` object. Others will be left as text, like ``~Other``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HeaderItem(mnemonic=VERS, unit=, value=2.0, descr=CWLS LOG ASCII STANDARD - ...),\n",
       " HeaderItem(mnemonic=WRAP, unit=, value=NO, descr=ONE LINE PER DEPTH STEP)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use this like a list... or like a dictionary, using the ``mnemonic=`` attribute as a key to the dictionary.\n",
    "\n",
    "for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HeaderItem(mnemonic=VERS, unit=, value=2.0, descr=CWLS LOG ASCII STANDARD - ...)\n",
      "HeaderItem(mnemonic=WRAP, unit=, value=NO, descr=ONE LINE PER DEPTH STEP)\n"
     ]
    }
   ],
   "source": [
    "for item in las.version:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or we can pull out an item using the section like a dictionary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HeaderItem(mnemonic=VERS, unit=, value=2.0, descr=CWLS LOG ASCII STANDARD - ...)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.version['VERS']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or even as an attribute, if the mnemonic allows it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HeaderItem(mnemonic=VERS, unit=, value=2.0, descr=CWLS LOG ASCII STANDARD - ...)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.version.VERS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each ``HeaderItem`` is also itself an [attribute dictionary](https://pypi.python.org/pypi/attrdict) - very easy to get at each property either as an item (dict style) or an attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'CWLS LOG ASCII STANDARD - VERSION 2.0'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = las.version.VERS\n",
    "v.descr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v.value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Moving on to the standard ``~Well`` section of a LAS header.\n",
    "\n",
    "Let's say we'd like to edit a property. We can do it easily by directly assigning the values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HeaderItem(mnemonic=CTRY, unit=, value=, descr=)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.well.CTRY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HeaderItem(mnemonic=CTRY, unit=, value=, descr=Australia)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.well.CTRY.descr = 'Australia'\n",
    "las.well.CTRY"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's have a look at the curves section."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[CurveItem(mnemonic=DEPT, unit=M, value=, descr=DEPTH, original_mnemonic=DEPT, data.shape=(121,)),\n",
       " CurveItem(mnemonic=CALI, unit=MM, value=, descr=CALI, original_mnemonic=CALI, data.shape=(121,)),\n",
       " CurveItem(mnemonic=DFAR, unit=G/CM3, value=, descr=DFAR, original_mnemonic=DFAR, data.shape=(121,)),\n",
       " CurveItem(mnemonic=DNEAR, unit=G/CM3, value=, descr=DNEAR, original_mnemonic=DNEAR, data.shape=(121,)),\n",
       " CurveItem(mnemonic=GAMN, unit=GAPI, value=, descr=GAMN, original_mnemonic=GAMN, data.shape=(121,)),\n",
       " CurveItem(mnemonic=NEUT, unit=CPS, value=, descr=NEUT, original_mnemonic=NEUT, data.shape=(121,)),\n",
       " CurveItem(mnemonic=PR, unit=OHM/M, value=, descr=PR, original_mnemonic=PR, data.shape=(121,)),\n",
       " CurveItem(mnemonic=SP, unit=MV, value=, descr=SP, original_mnemonic=SP, data.shape=(121,)),\n",
       " CurveItem(mnemonic=COND, unit=MS/M, value=, descr=COND, original_mnemonic=COND, data.shape=(121,))]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.curves"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "``CurveItem``s are very similar. The only difference is that they have an additional ``data=`` attribute."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice the units for PR are incorrect - we can fix that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "las.curves.PR.unit = \"ohmm\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CurveItem(mnemonic=PR, unit=ohmm, value=, descr=PR, original_mnemonic=PR, data.shape=(121,))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.curves[\"PR\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And finally the standard ``~Parameter`` section:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HeaderItem(mnemonic=BS, unit=, value=216 mm, descr=BS),\n",
       " HeaderItem(mnemonic=JOBN, unit=, value=, descr=JOBN),\n",
       " HeaderItem(mnemonic=WPMT, unit=, value=, descr=WPMT),\n",
       " HeaderItem(mnemonic=AGL, unit=, value=, descr=AGL),\n",
       " HeaderItem(mnemonic=PURP, unit=, value=Cased hole stratigraphy, descr=PURP),\n",
       " HeaderItem(mnemonic=X, unit=, value=560160, descr=X),\n",
       " HeaderItem(mnemonic=CSGL, unit=, value=0 m - 135 m, descr=CSGL),\n",
       " HeaderItem(mnemonic=UNIT, unit=, value=, descr=UNIT),\n",
       " HeaderItem(mnemonic=Y, unit=, value=6686430, descr=Y),\n",
       " HeaderItem(mnemonic=TDL, unit=, value=135.2 m, descr=TDL),\n",
       " HeaderItem(mnemonic=PROD, unit=, value=, descr=PROD),\n",
       " HeaderItem(mnemonic=MUD, unit=, value=Water, descr=MUD),\n",
       " HeaderItem(mnemonic=CSGS, unit=, value=100 mm, descr=CSGS),\n",
       " HeaderItem(mnemonic=ENG, unit=, value=, descr=ENG),\n",
       " HeaderItem(mnemonic=STEP, unit=, value=5 cm, descr=STEP),\n",
       " HeaderItem(mnemonic=FLUIDLEVEL, unit=, value=54 m, descr=FluidLevel),\n",
       " HeaderItem(mnemonic=CSGT, unit=, value=PVC, descr=CSGT),\n",
       " HeaderItem(mnemonic=WIT, unit=, value=, descr=WIT),\n",
       " HeaderItem(mnemonic=EREF, unit=, value=, descr=EREF),\n",
       " HeaderItem(mnemonic=PROJ, unit=, value=, descr=PROJ),\n",
       " HeaderItem(mnemonic=ZONE, unit=, value=53J, descr=ZONE),\n",
       " HeaderItem(mnemonic=DREF, unit=, value=GL, descr=DREF),\n",
       " HeaderItem(mnemonic=TDD, unit=, value=136 m, descr=TDD)]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.params         # las.sections[\"params\"] would also work"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you'd prefer to see a section as a dictionary with the ``value`` property, you can with the ``dictview()`` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'BS': '216 mm',\n",
       " 'JOBN': '',\n",
       " 'WPMT': '',\n",
       " 'AGL': '',\n",
       " 'PURP': 'Cased hole stratigraphy',\n",
       " 'X': 560160,\n",
       " 'CSGL': '0 m - 135 m',\n",
       " 'UNIT': '',\n",
       " 'Y': 6686430,\n",
       " 'TDL': '135.2 m',\n",
       " 'PROD': '',\n",
       " 'MUD': 'Water',\n",
       " 'CSGS': '100 mm',\n",
       " 'ENG': '',\n",
       " 'STEP': '5 cm',\n",
       " 'FLUIDLEVEL': '54 m',\n",
       " 'CSGT': 'PVC',\n",
       " 'WIT': '',\n",
       " 'EREF': '',\n",
       " 'PROJ': '',\n",
       " 'ZONE': '53J',\n",
       " 'DREF': 'GL',\n",
       " 'TDD': '136 m'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "las.params.dictview()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
